package com.baichuan.util;

import org.bouncycastle.util.encoders.Hex;

import java.nio.charset.Charset;

public class SimpleEncrypt {

    private static final String key0 = "FECOI()*&<MNCXZPKL";
    private static final Charset charset = Charset.forName("UTF-8");
    private static byte[] keyBytes = key0.getBytes(charset);

    /**
     * 加密
     *
     * @param enc
     * @return
     */
    public static String encrypt(String enc) {
        byte[] b = enc.getBytes(charset);
        for (int i = 0, size = b.length; i < size; i++) {
            for (byte keyBytes0 : keyBytes) {
                b[i] = (byte) (b[i] ^ keyBytes0);
            }
        }
        return new String(Hex.encode(b), charset);
    }

    /**
     * 解密
     *
     * @param dec
     * @return
     */
    public static String decrypt(String dec) {
        try {
            byte[] e = Hex.decode(dec);
            for (int i = 0, size = e.length; i < size; i++) {
                for (byte keyBytes0 : keyBytes) {
                    e[i] = (byte) (e[i] ^ keyBytes0);
                }
            }
            return new String(e, charset);
        }catch ( Exception e){
            return null;
        }

    }
}